/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | foam-extend: Open Source CFD
   \\    /   O peration     | Version:     4.1
    \\  /    A nd           | Web:         http://www.foam-extend.org
     \\/     M anipulation  | For copyright notice see file Copyright
-------------------------------------------------------------------------------
License
	This file is part of foam-extend.

	foam-extend is free software: you can redistribute it and/or modify it
	under the terms of the GNU General Public License as published by the
	Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	foam-extend is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "processorLduInterface.H"
#include "IPstream.H"
#include "OPstream.H"

// * * * * * * * * * * * * * * * Member Functions * * *  * * * * * * * * * * //

template<class Type>
void Foam::processorLduInterface::send
(
	const Pstream::commsTypes commsType,
	const UList<Type>& f
) const
{
	label nBytes = f.byteSize();

	if (commsType == Pstream::blocking || commsType == Pstream::scheduled)
	{
		OPstream::write
		(
			commsType,
			neighbProcNo(),
			reinterpret_cast<const char*>(f.begin()),
			nBytes,
			tag(),
			comm()
		);
	}
	else if (commsType == Pstream::nonBlocking)
	{
		resizeBuf(receiveBuf_, nBytes);

		IPstream::read
		(
			commsType,
			neighbProcNo(),
			receiveBuf_.begin(),
			nBytes,
			tag(),
			comm()
		);

		resizeBuf(sendBuf_, nBytes);
		memcpy(sendBuf_.begin(), f.begin(), nBytes);

		OPstream::write
		(
			commsType,
			neighbProcNo(),
			sendBuf_.begin(),
			nBytes,
			tag(),
			comm()
		);
	}
	else
	{
		FatalErrorIn("processorLduInterface::send")
			<< "Unsupported communications type " << commsType
			<< exit(FatalError);
	}
}


template<class Type>
void Foam::processorLduInterface::receive
(
	const Pstream::commsTypes commsType,
	UList<Type>& f
) const
{
	if (commsType == Pstream::blocking || commsType == Pstream::scheduled)
	{
		IPstream::read
		(
			commsType,
			neighbProcNo(),
			reinterpret_cast<char*>(f.begin()),
			f.byteSize(),
			tag(),
			comm()
		);
	}
	else if (commsType == Pstream::nonBlocking)
	{
		memcpy(static_cast<void*>(f.begin()), receiveBuf_.begin(), f.byteSize());
	}
	else
	{
		FatalErrorIn("processorLduInterface::receive")
			<< "Unsupported communications type " << commsType
			<< exit(FatalError);
	}
}


template<class Type>
Foam::tmp<Foam::Field<Type> > Foam::processorLduInterface::receive
(
	const Pstream::commsTypes commsType,
	const label size
) const
{
	tmp<Field<Type> > tf(new Field<Type>(size));
	receive(commsType, tf());
	return tf;
}


// ************************************************************************* //
